let Mock = {
  timeout: "1000-3000", //请求间隔时间
  intercepts: [],
  mock: (regex, method, response) => {
    Mock.intercepts.push({
      regex: regex,
      method: method,
      response: response
    })
  },
  response: config => {
    return new Promise((resolve, reject) => {
      const delay = Math.floor(Math.random() * Mock.timeout.split("-")[0] + Mock.timeout.split("-")[1]);
      setTimeout(function () {
        let findIntercept = null; //声明一个存储的空值
        let maker = false;
        for (const key in Mock.intercepts) {
          let intercept = Mock.intercepts[key];
          console.log(intercept.method)
          if (intercept.regex.test(config.url) && intercept.method === config.method.toLowerCase()) {
            maker = true;
            findIntercept = intercept;
          }
        }
        if (maker) {
          resolve({
            statusCode: "200",
            data: findIntercept.response(config)
          });
        } else {
          reject(`未找到对应${config.method} ${config.url}的模拟响应`);
        }
      }, delay)
    })
  }
}
import DouBan from './douban'
if (process.env.NODE_ENV === 'development') {
  Mock.mock(/\/top250/, 'get', DouBan.douban)
}
export default Mock;
